package taxicall.server;

import java.io.IOException;
import java.io.Serializable;
import java.net.InetAddress;
import java.util.ArrayList;
import java.util.List;

import com.taxicall.entities.NetworkMessage;
import com.taxicall.entities.NetworkMessage.MessageType;
import com.taxicall.entities.Taxi;
import com.taxicall.entities.TaxiLocation;
import com.taxicall.udptransport.UdpSender;

/**
 * All the server operations are here.
 */
public class ServerController {
	
	public TaxiRepository taxiRepository = new TaxiRepository();
	
	private UdpSender udpSender;
	
	public ServerController(UdpSender udpSender) {
		this.udpSender = udpSender;
	}

	public void getStartLocations(InetAddress fromAddress) {
		List<TaxiLocation> locations = new ArrayList<TaxiLocation>();
		locations.add(new TaxiLocation("Sintagma", "Plateia Sintagmatos", 37.975751, 23.733833));
		locations.add(new TaxiLocation("Omonoia", "Plateia Omonoias", 37.983742, 23.727965));
		locations.add(new TaxiLocation("Siggrou-Fix", "Kallirois kai Irakleous", 37.964491,23.727101));
		locations.add(new TaxiLocation("Kallithea", "Stathmos ISAP Kallitheas", 37.959849,23.69798));
		locations.add(new TaxiLocation("Peiraias", "Stathmos ISAP Peiraia", 37.948306,23.642443));
		locations.add(new TaxiLocation("Glifada", "Plateia Katraki", 37.863573,23.746651));
		locations.add(new TaxiLocation("Alimos", "Plateia Ampelakiwn", 37.917261,23.722731));
		locations.add(new TaxiLocation("Ethniki Amina", "Lewforos Mesogeiwn", 37.999759,23.785554));
		locations.add(new TaxiLocation("Aigalew", "Stathmos Metro Aigalew", 37.991307,23.68239));
		locations.add(new TaxiLocation("Gazi", "Plateia Kerameikou", 37.978863, 23.71038));
		locations.add(new TaxiLocation("Peristeri", "Alsos Peristeriou", 38.016671,23.687257));
		locations.add(new TaxiLocation("Galatsi", "Galatsiou kai Veikou", 38.013064,23.752957));
		locations.add(new TaxiLocation("Chalandri", "Plateia Chalandriou", 38.021779,23.797305));
		locations.add(new TaxiLocation("N. Irakleio", "Stathmos ISAP N. Irakleiou", 38.046213,23.765912));
		locations.add(new TaxiLocation("Kifisia", "Stathmos ISAP Kifisias", 38.073631,23.808493));
		locations.add(new TaxiLocation("Acharnes", "Plateia Ethnikis Antistasews", 38.082421,23.733295));
		locations.add(new TaxiLocation("Melissia", "Dimokratias kai 25is Martiou",38.051449,23.832008));
		locations.add(new TaxiLocation("Dafni", "Stathmos Metro Dafnis", 37.949703,23.738025));
		locations.add(new TaxiLocation("Nikaia", "Dimotiko Theatro Kipou", 37.970603,23.640647 ));
		send(MessageType.SetStartLocations, (Serializable) locations, fromAddress);
	}
	
	public void getAvailableTaxis(InetAddress fromAddress){
		List<Taxi> taxis = new ArrayList<Taxi>();
		for(Taxi t : taxiRepository.getAll()){
			if(t.getAvailableSeats() > 0){
				taxis.add(t);
			}
		}
		
		send(MessageType.SetAllTaxis, (Serializable) taxis, fromAddress);
	}
	
	public void updateTaxi(Taxi taxi, InetAddress fromAddress) {
		taxiRepository.addOrUpdate(taxi);
		send(MessageType.UpdateTaxiOk, taxi, fromAddress);
	}
	
	// Network sending utils
	private void send(MessageType type, Serializable data, InetAddress toAddress){
		send(type, data, toAddress, Server.PORT);
	}
	
	private void send(MessageType type, Serializable data, InetAddress toAddress,  int port){
		NetworkMessage msg = new NetworkMessage(MessageType.SetStartLocations, data);
		try {
			udpSender.send(msg, toAddress, Server.PORT);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
